package com.cj.bus.common.entity.tree;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;


public class MultiTreeNode {

	    private int id;
	    private String text;
	    private List<MultiTreeNode> nodes;
	    private String childIds;
	    
	    
	    public MultiTreeNode() {
	    }
	    public MultiTreeNode(int id, String text) {
	        this.id = id;
	        this.text = text;
	    }
	    public MultiTreeNode(int id, String text, List<MultiTreeNode> nodes) {
	        this.id = id;
	        this.text = text;
	        this.nodes = nodes;
	    }
	    public int getId() {
	        return id;
	    }
	    public void setId(int id) {
	        this.id = id;
	    }
	    
	    public String getText() {
			return text;
		}
		public void setText(String text) {
			this.text = text;
		}
		
		
		public List<MultiTreeNode> getNodes() {
			return nodes;
		}
		public void setNodes(List<MultiTreeNode> nodes) {
			this.nodes = nodes;
		}
		
		public String getChildIds() {
			List<Integer> child = new ArrayList<Integer>();
			child.add(id);
			if(!CollectionUtils.isEmpty(nodes)) {
				for(MultiTreeNode node : nodes) {
					findChild(node,child);
				}
			}
			return StringUtils.join(child.toArray(), ",");
		}
		
		private void findChild(MultiTreeNode node,List<Integer> child) {
			child.add(node.getId());
			if(!CollectionUtils.isEmpty(node.getNodes())) {
				for(MultiTreeNode one : node.getNodes()) {
					findChild(one,child);
				}
			}
		}
		
		public void setChildIds(String childIds) {
			this.childIds = childIds;
		}
		@Override
	      public String toString() {
	        return "MultiTreeNode [id=" + id + ", text=" + text + ", nodes=" + nodes + "]";
	    }
}
